﻿/**
 * ...
 * @author	Kliment
 * @version	1.0.1
 */
class kliment.utils._Array {
	
	public static function remove(value, target:Array):Boolean {
		var length:Number = target.length;
		for (var i:Number = 0; i < length; i++) 
			if (target[i] == value){
				target.splice(i, 1);
				return true;
			}
		return false;
	}
	
	public static function last(value:Array) {
		return value[lastIndex(value)];
	}
	
	public static function lastIndex(value:Array):Number {
		return value.length - 1;
	}
	
	public static function multiply(value:Number, array:Array):Array {
		var temp:Array = new Array();
		for (var item in array) {
			if(item == 0)
				temp.push(0);
			else
				temp.push(value * item);
		}
		return temp;
	}
	
	static public var sortCondition:Function = function(val1, val2):Boolean { return false };
	
	static public function sortInsertion(array:Array, sortCondition:Function):Array {
		var length:Number = array.length;
		var temp:Array = new Array();
		for (var i:Number = 0; i < length; i++){
			var j:Number = i;
			while (j > 0 && sortCondition(temp[j-1], array[i])){
				temp[j] = temp[j-1];
				j--;
			}
			temp[j] = array[i];
		}
		return temp;
	}
	
	static public function indexOf(array:Array, value):Number {
		var length:Number = array.length;
		var result:Number = -1
		for (var i:Number = 0; i < length; ++i) {
			if (array[i] == value) {
				result = i;
				break;
			}
		}
		return result;
	}
}